package com.kitking.myapplication.utils;

import android.util.Log;


import com.kitking.myapplication.lister.HttpCallbackLister;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {
    //有回调
    public static void sendHttpRequest2(final String address, final HttpCallbackLister lister) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    if (lister != null) {
                        lister.onFinsh(response.toString());
                    }

                } catch (Exception e) {
                    if (lister != null) {
                        lister.onError(e);
                    }
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }


            }
        }).start();

    }

    //没有回调
    public static void sendHttpRequest(String address) {

        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpURLConnection connection = null;
                try {
                    Log.d("111111-----", "11111111111111");

                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    Log.d("111111-----", "11111111111111" + response.toString());
                    //直接用个子线程开启网络请求操作，也能行但是run方法没法返回数据，所以需要回调函数
                    // return response.toString();

                } catch (Exception e) {
                    e.printStackTrace();
                    // return e.getMessage();
                } finally {

                    if (connection != null) {
                        Log.d("2222222-----", "2222222222");

                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

}
